'use strict'
/**
 * 注册全局自定义指令
 * require.context(directory, useSubdirectories, regExp)
 * directory: 要查找的文件路径
 * useSubdirectories: 是否查找子目录
 * regExp: 要匹配文件的正则
 */
const requireFunction = require.context('./', true, /\.(js|ts)$/)
const install = (Vue) => {
  const log = []
  requireFunction.keys().forEach(fileName => {
    const config = requireFunction(fileName)
    let prototypeName = fileName.match(/.*\/([^.]+)/)
    if (prototypeName && prototypeName[1] !== 'index') {
      prototypeName = prototypeName[1]
      log.push(`【${prototypeName}】 组件内部 v-${prototypeName}`)
      Vue.directive(prototypeName, config.default || config)
    }
  })
  console.info(
    `%c 指令注册成功 %c 无需单独引入，页面组件直接使用，如下：%c`,
    'background:#35495e; padding: 2px; border-radius: 3px 0 0 3px;  color: #fff',
    'background: rgba(65,184,131, 1); padding: 2px; border-radius: 0; letter-spacing: 2px; color: rgba(0,0,0, .5)',
    'background: transparent'
  )
  console.log(log)
  if (typeof window !== 'undefined' && window.Vue) {
    install(window.Vue)
  }
}
export default install
